﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Box.Entities;
namespace Box.Repository
{
    public class PagosRepository : MasterRepository, IPagosRepository
    {
        public List<Pagos> GetAllFromPagos()
        {
            var query = from c in Context.Pagos
                        .Include("Proveedor")
                        select c;
            return query.ToList();
        }

        public List<Pagos> GetFromPagosByCriterio(string criterio)
        {
            var query = from c in Context.Pagos
                        .Include("Proveedor")
                        select c;
            if (criterio != null && criterio != string.Empty)
            {
                query = from p in query
                    where p.Proveedor.NombreEmpresa.ToUpper().Contains(criterio.ToUpper())
                    select p;
            }
            return query.ToList();
        }

        public Pagos GetFromPagosById(int id)
        {
            var query = from p in Context.Pagos
                .Include("Proveedor")
                select p;
            if (id!=null)
            {
                query = from p in query
                    where p.Id.Equals(id)
                    select p;
            }
            return query.SingleOrDefault();
        }

        public void AddPagos(Pagos pagos)
        {
            Context.Pagos.Add(pagos);
            Context.SaveChanges();
        }

        public void UpdatePagos(Pagos Pagos)
        {
            Context.Entry(Pagos).State = EntityState.Modified;
            Context.SaveChanges();
        }

        public void RemovePagos(int id)
        {
            var existe = Context.Pagos.Find(id);
            if (existe != null)
            {
                Context.Pagos.Remove(existe);
                Context.SaveChanges();
            }
        }

        public bool ExistePagos(int id)
        {
           var query = Context.Pagos.Where(p => p.Id.Equals(id));

            return query.Any();
        }
    }
}
